package com.marketapp.service;

import java.util.List;

import org.apache.commons.collections.HashBag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.marketapp.dao.IEquityDao;
import com.marketapp.domain.Equity;
import com.marketapp.domain.Sector;
import com.marketapp.helper.DaoException;
import com.marketapp.helper.ServiceException;

public class EquityServiceImpl implements IEquityService {

	@Autowired
	@Qualifier(value = "equityDao")
	IEquityDao equityDao;

	public IEquityDao getEquityDao() {
		return equityDao;
	}

	public void setEquityDao(IEquityDao equityDao) {
		this.equityDao = equityDao;
	}

	@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED, readOnly = true)
	public List<Equity> getEquityDetailsbyName(String equityName)
			throws ServiceException {
		try {
			List<Equity> resultList = equityDao.getAllEquityByName(equityName);
			return resultList;
		} catch (Exception e) {
			throw new ServiceException("Error: Could not retrieve data.",
					e.getCause());
		}
	}

	@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED)
	public void saveEquity(Equity equity) throws ServiceException {
		try {
			equityDao.saveEquity(equity);
		} catch (Exception e) {
			throw new ServiceException("Error: Could not save equity "
					+ equity.getEquityName(), e.getCause());
		}
	}

	@Transactional(propagation=Propagation.REQUIRED,isolation=Isolation.READ_COMMITTED)
	public boolean checkifEquityExist(String equityBSE, String equityNSE,
			String equityISIN) throws ServiceException {
		List<Equity> equityList;
		try {
			equityList = equityDao.checkIfEquityExists(equityBSE, equityNSE,
					equityISIN);
			return equityList.isEmpty() ? false : true;
		} catch (DaoException e) {
			throw new ServiceException("Error:Could not get data", e.getCause());
		}
	}
}
